const route = require('koa-route');
const resitDatabase = require('../resitDatabase');

const resName = '/resits';

// GET v1/resits/:stuID
module.exports.resits = {
    v1: route.get(`/v1${resName}/:stuID`, async (ctx, stuID, next) => {

        if (!/^(2[0,1,3]\d{7})$/.test(stuID)) {
            ctx.status = 400;
            return ctx.body = {
                error: '学号格式不正确'
            }
        }

        // 查询补考信息
        let resits = await resitDatabase.findResits(stuID);
        // 判断是否没有补考
        if (0 === resits.length) {
            ctx.status = 404;
            return ctx.body = {
                error: '没有找到这个学号的补考信息'
            }
        }

        // 成功找到这个学号的补考信息
        resits = {
            stuID : resits[0].stuID,
            stuName: resits[0].stuName,
            college : resits[0].college,
            'class': resits[0].class,
            resits: resits
        }
        resits = JSON.parse(JSON.stringify(resits));
        for (let i of resits.resits) {
            delete i.stuID;
            delete i.stuName;
            delete i.college;
            delete i.class;
        }
        // 返回补考信息
        return ctx.body = resits;
    })
}